package com.chookss.push;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.Utils;
import com.johnrambo.ktea.common.klogutil.KLog;

import java.util.Map;

public class MyMessageReceiver extends MessageReceiver {
    // 消息接收部分的LOG_TAG
    public static final String REC_TAG = "receiver";

    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        //用于接收服务端推送的通知，通知会弹窗，通知接收通知以及点击通知等都有回调。
        Log.e("MyMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
        createNotifiction(context, title, summary, extraMap);
    }

    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        // 用于接收服务端推送的消息，消息不会弹窗，而是回调该方法
        Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
//        createNotifiction(context,cPushMessage.getTitle(),cPushMessage.getContent());
    }

    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }

    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }

    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }

    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        Log.e("MyMessageReceiver", "onNotificationRemoved");
    }

    private void createNotifiction(Context context, String title, String summary, Map<String, String> extraMap) {
        //1. main -> 2.web
        Object o = context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (o instanceof NotificationManager) {
            Intent intent =new Intent (context,NotificationClickReceiver.class);
            String type = "", code = "", pushMessageId = "";
            int id = 2;
            KLog.i("MyMessageReceiver", extraMap);
//            type类型：1、公告 2、新闻3、考试4、h5页面 5、面试待评价列表
            if (extraMap.containsKey("type")) {
                type = extraMap.get("type");
            }
            if (extraMap.containsKey("code")) {
                code = extraMap.get("code");
            }
            if (extraMap.containsKey("pushMessageId")) {
                pushMessageId = extraMap.get("pushMessageId");
            }
            if (extraMap.containsKey("_ALIYUN_NOTIFICATION_ID_")) {
                id = Utils.changeNumber(extraMap.get("_ALIYUN_NOTIFICATION_ID_"));
            }
            intent.putExtra("pushMessageId",pushMessageId);
            intent.putExtra("type",type);
            intent.putExtra("code",code);
            PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            NotificationManager notificationManager = (NotificationManager) o;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel("1", "推送通知", NotificationManager.IMPORTANCE_HIGH);
                //振动
                channel.enableVibration(true);
                channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                notificationManager.createNotificationChannel(channel);
            }
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "1");
            builder.setContentTitle(title);
            builder.setContentText(summary);
            builder.setSmallIcon(GlideUtils.loadPushId(context));
            //系统声音
            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            builder.setSound(uri);
            //开启用户滑动不会消失
//            builder.setOngoing(true);
//            用户点击Notification点击面板后是否让通知取消(默认不取消)
            builder.setAutoCancel(true);
            builder.setStyle(new NotificationCompat.BigTextStyle().bigText(summary));
            builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
//            //设置通知的动作
            builder.setContentIntent(mPendingIntent)
                    //设置通知时间，默认为系统发出通知的时间
                    .setWhen(System.currentTimeMillis());
            notificationManager.notify(id, builder.build());
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }

}
